﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using Microsoft.Practices.Prism.Commands;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public class OrderDetailViewModel : ViewModelBase
    {
        public OrderDetailViewModel()
        {
            Model = new OrderDetailModel();
            ((OrderDetailModel)Model).GetSingleOrderCompleted += DoGetSingleOrderCompleted;
        }

        #region ImplementEventCompleted
       
        void DoGetSingleOrderCompleted(object sender, SingleFeedbackCompletedEventArgs<Order> e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                MessageVisibility = Visibility.Visible;
            }
            else
            {
                Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    MessageVisibility = Visibility.Visible;
                }
                else
                {

                    CollectionMaterial = e.Result.Data.MaterialCollection;
                    if (CollectionMaterial != null)
                    {
                        PagedCollectionMaterial = new PagedCollectionView(CollectionMaterial);
                        WarehouseName = CollectionMaterial[0].Warehouse.WarehouseName;
                    }
                    MaterialOrder = e.Result.Data;
                    
                    if (e.Result.Data.IsImported == true)
                    {
                        ImportExport = "Nhập kho";
                    }
                    else
                    {
                        ImportExport = "Xuất kho";
                    }

                    if (MaterialOrder.Contact.ContactID == 0)
                    {
                        WorkerContact = "Công nhân";
                        IsInternal = true;
                        IsExternal = false;
                    }
                    else
                    {
                        WorkerContact = "Khách hàng";
                        IsInternal = false;
                        IsExternal = true;
                    }
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;

        }
        #endregion
        
        //Back command use to turn back list order screeen
        private DelegateCommand<object> cancelCommand;
        public DelegateCommand<object> CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new DelegateCommand<object>(DoBackCommand);
                }
                return cancelCommand;
            }
        }
        private void DoBackCommand(object obj)
        {
            NavigationManager.NavigateTo(WebConstants.NavigationPages.MaterialOrderList);
        }
        
        

        #region MaterialOrder
        private Order materialOrder;
        public Order MaterialOrder
        {
            get { return materialOrder; }
            set
            {
                if (!ReferenceEquals(materialOrder, value))
                {
                    materialOrder = value;
                    RaisePropertyChanged(() => MaterialOrder);
                }
            }
        }

        #endregion

        #region Collection Material
        private ObservableCollection<Material> collectionMaterial;
        public ObservableCollection<Material> CollectionMaterial
        {
            get { return collectionMaterial; }
            set
            {
                if (!ReferenceEquals(collectionMaterial, value))
                {
                    collectionMaterial = value;
                    RaisePropertyChanged(() => CollectionMaterial);
                }
            }
        }
        #endregion

        private PagedCollectionView pagedCollectionMaterial;
        public PagedCollectionView PagedCollectionMaterial
        {
            get { return pagedCollectionMaterial; }
            set
            {
                if (!ReferenceEquals(pagedCollectionMaterial, value))
                {
                    pagedCollectionMaterial = value;
                    RaisePropertyChanged(() => PagedCollectionMaterial);
                }
            }
        }
        #region Implementation of INavigationAware

        public override void OnNavigate(object extraData)
        {
            var nd = (NavigationData)extraData;
            if (nd != null)
            {
                if (nd.NavigationUri.Equals(WebConstants.NavigationPages.MaterialOrderList))
                {
                   ServiceCallCounter += ((OrderDetailModel)Model).GetSingleOrderAsync((int)nd.Data);
                   
                }
            }
        }

        public override void OnLeave()
        {
            //will be implemented in the sub classes
        }

        #endregion

        private string warehouseName;
        public string WarehouseName
        {
            get { return warehouseName; }
            set
            {
                warehouseName = value;
                RaisePropertyChanged(() => WarehouseName);

            }
        }
        private string importExport;
        public string ImportExport
        {
            get { return importExport; }
            set
            {
                importExport = value;
                RaisePropertyChanged(() => ImportExport);

            }
        }
        private string workerContact;
        public string WorkerContact
        {
            get { return workerContact; }
            set
            {
                workerContact = value;
                RaisePropertyChanged(() => WorkerContact);

            }
        }
        private bool isImported;
        public bool IsImported
        {
            get { return isImported; }
            set
            {
                isImported = value;
                RaisePropertyChanged(() => IsImported);

            }
        }
        private bool isExported;
        public bool IsExported
        {
            get { return isExported; }
            set
            {
                isExported = value;
                RaisePropertyChanged(() => IsExported);

            }
        }
        private bool isInternal;
        public bool IsInternal
        {
            get { return isInternal; }
            set
            {
                isInternal = value;
                RaisePropertyChanged(() => IsInternal);

            }
        }
        private bool isExternal;
        public bool IsExternal
        {
            get { return isExternal; }
            set
            {
                isExternal = value;
                RaisePropertyChanged(() => IsExternal);

            }
        }
    }
}
